package util

import (
	"crawl_movie/models"
	"fmt"
	"github.com/astaxie/beego/httplib"
	"math/rand"
	"net/http"
	"net/url"
	"os"
	"strconv"
	"strings"
	"time"
)

const ProxyServer = "u5918.b5.t.16yun.cn:6460"

var IsRun = false

func GetProxyURL() *url.URL {
	var proxyURL *url.URL
	proxyURL, _ = url.Parse("http://16PSVRSG:780248@" + ProxyServer)
	return proxyURL
}

func GetMovieInfo()  {
	models.ConnRedis("127.0.0.1:6379")
	urls := []string{"https://movie.douban.com/subject/26100958/", "https://movie.douban.com/subject/1292722/", "https://movie.douban.com/subject/25662329/",
		"https://movie.douban.com/subject/26683290/", "https://movie.douban.com/subject/1291561/"}
	for _, v := range urls {
		models.PutinQueue(v)
		models.AddToSet(v)
	}
	rand.Seed(time.Now().UnixNano())
	for {
		if IsRun {
			time.Sleep(time.Second * 30)
			continue
		}
		queue := models.PopFromQueue()
		html, err := httplib.Get(queue).SetProxy(http.ProxyURL(GetProxyURL())).String()
		if err != nil {
			fmt.Println("error : ", err)
		}
		if html == "" {
			models.PutinQueue(queue)
			time.Sleep(time.Second * 30)
			continue
		}else if strings.Contains(html[:100], "检测到有异常请求从你的 IP 发出") || strings.Contains(html[:20], "<script>") {
			models.PutinQueue(queue)
			time.Sleep(time.Second * 30)
			continue
		}
		movieInfo := models.MovieInfo{
			MovieId: 		models.GetMovieId(html),
			MovieName: 		models.GetMovieName(html),
			MoviePic: 		models.GetMoviePic(html),
			MovieDirector: 	models.GetMovieDirector(html),
			MovieWriter: 	models.GetMovieWrite(html),
			MovieCountry: 	models.GetMovieCountry(html),
			MovieLanguage: 	models.GetMovieLanguage(html),
			MovieMainCharacter: models.GetMovieCharacters(html),
			MovieType: 		models.GetMovieType(html),
			MovieOnTime:	models.GetMovieOnTime(html),
			MovieSpan:		models.GetMovieSpan(html),
			MovieGrade: 	models.GetMovieGrade(html),
			MovieSynopsis: 	models.GetMovieSynopsis(html),
		}
		picName := "./static/img/movie/" + strconv.Itoa(int(movieInfo.MovieId)) + ".jpg"
		if movieInfo.MoviePic != "" {
			img, err := httplib.Get(movieInfo.MoviePic).Bytes()
			if err != nil {
				fmt.Println("ERROR : ", err)
			}
			file, _ := os.OpenFile(picName, os.O_CREATE|os.O_WRONLY, 0644)
			_, _ = file.Write(img)
			file.Close()
			movieInfo.MoviePic = picName[1:]
		}
		models.AddMovieByMovieId(&movieInfo)
		time.Sleep(time.Second * time.Duration(rand.Intn(10)))
		for _, u := range models.GetMovieUrls(html) {
			if !models.IsVisit(u) {
				models.PutinQueue(u)
				models.AddToSet(u)
			}
		}
		if models.GetQueueLength() == 0 {
			time.Sleep(time.Second * 30)
			continue
		}
	}

}